/**
 * 
 */
package sudoku.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import sudoku.util.Config;
import sudoku.util.Language;

/**
 * @author rometp
 * 
 */
public class FontSizeUI implements ActionListener, ChangeListener {

  private volatile SudokuUI m2ng;
  private volatile Config config;
  private volatile Language language;

  private JSlider slider9;
  private JSlider slider16;

  private JRadioButton game9;
  private JRadioButton game16;

  private JButton b1;
  private JButton b2;

  private JButton ok;
  private JButton cancel;

  private int lastValue9;
  private int lastValue16;

  private JDialog dialog;

  public FontSizeUI(SudokuUI m2ng, Config config, Language language) {
    this.m2ng = m2ng;
    this.config = config;
    this.language = language;

    drawWindow();
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource().equals(ok)) {
      save();
      dialog.dispose();
    }
    else if (e.getSource().equals(cancel)) {
      dialog.dispose();
    }
    else if (e.getSource().equals(game9)) {
      slider9.setVisible(true);
      slider16.setVisible(false);
      lastValue9 = slider9.getValue();
      setExample(9);
      dialog.pack();
      for (int k = 0; k < 9; k++) {
        ((JLabel) b2.getComponent(k)).setText("" + (k + 1));
      }
    }
    else if (e.getSource().equals(game16)) {
      slider9.setVisible(false);
      slider16.setVisible(true);
      lastValue16 = slider16.getValue();
      setExample(16);
      dialog.pack();
      for (int k = 0; k < 16; k++) {
        ((JLabel) b2.getComponent(k)).setText("" + (k + 1));
      }
    }
  }

  @Override
  public void stateChanged(ChangeEvent e) {
    JSlider source = (JSlider) e.getSource();
    if (!source.getValueIsAdjusting()) {
      if (slider9.isVisible()) {
        setExample(9);
        dialog.pack();
        for (int k = 0; k < 9; k++) {
          ((JLabel) b2.getComponent(k)).setText("" + (k + 1));
        }
      }
      else {
        setExample(16);
        dialog.pack();
        for (int k = 0; k < 16; k++) {
          ((JLabel) b2.getComponent(k)).setText("" + (k + 1));
        }
      }
    }
  }

  public void drawWindow() {
    dialog = new JDialog(m2ng, language.getSudoku_fonts_menu(), true);
    dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    game9 = new JRadioButton("9x9" + language.getFontSize_message(), true);
    game9.addActionListener(this);
    game16 = new JRadioButton("16x16" + language.getFontSize_message(), false);
    game16.addActionListener(this);
    ButtonGroup bg = new ButtonGroup();
    bg.add(game9);
    bg.add(game16);

    Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>();
    labelTable.put(new Integer(-3), new JLabel("Min"));
    labelTable.put(new Integer(6), new JLabel("Max"));
    labelTable.put(new Integer(1), new JLabel("Defalt"));

    lastValue9 = config.getFont9();
    lastValue16 = config.getFont16();
    slider9 = new JSlider(-3, 6, config.getFont9());
    slider9.setSnapToTicks(true);
    slider9.setMajorTickSpacing(1);
    slider9.setPaintTicks(true);
    slider9.setLabelTable(labelTable);
    slider9.setPaintLabels(true);
    slider9.addChangeListener(this);
    slider9.setVisible(true);

    slider16 = new JSlider(-3, 6, config.getFont16());
    slider16.setSnapToTicks(true);
    slider16.setMajorTickSpacing(1);
    slider16.setPaintTicks(true);
    slider16.setLabelTable(labelTable);
    slider16.setPaintLabels(true);
    slider16.addChangeListener(this);
    slider16.setVisible(false);

    b1 = new JButton("9");
    b1.setMinimumSize(new Dimension(56, 60));
    b2 = new JButton(" ");
    setExample(9);
    JPanel panel2 = new JPanel(new GridLayout(1, 2));
    panel2.add(b1);
    panel2.add(b2);

    JLabel label = new JLabel(language.getFontSize_examples());

    JPanel panel = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 0;
    panel.add(game9, c);
    c.gridx = 0;
    c.gridy = 1;
    panel.add(game16, c);
    c.gridx = 0;
    c.gridy = 2;
    panel.add(slider9, c);
    c.gridx = 0;
    c.gridy = 3;
    panel.add(slider16, c);
    c.gridx = 0;
    c.gridy = 4;
    panel.add(label, c);
    c.gridx = 0;
    c.gridy = 5;
    panel.add(panel2, c);
    JPanel p = new JPanel(new GridLayout(1, 2));
    ok = new JButton("OK");
    ok.addActionListener(this);
    cancel = new JButton(language.getButton_cancel());
    cancel.addActionListener(this);

    p.add(ok);
    p.add(cancel);
    dialog.add(panel, BorderLayout.CENTER);
    dialog.add(p, BorderLayout.SOUTH);
    dialog.pack();
    for (int k = 0; k < 9; k++) {
      ((JLabel) b2.getComponent(k)).setText("" + (k + 1));
    }
    dialog.setLocation(m2ng.getLocation().x + m2ng.getWidth() / 2 - dialog.getWidth() / 2, m2ng.getLocation().y + m2ng.getHeight() / 2 - dialog.getHeight() / 2);
    dialog.setVisible(true);
  }

  public void save() {
    config.setFonts(slider9.getValue(), slider16.getValue());
    m2ng.uuendaUI();
    m2ng.pack();
    m2ng.resizeM2ng();
  }

  private void setExample(int i) {
    int kast = (int) Math.round(Math.sqrt(i));
    if (i == 9) {
      b1.setFont(config.getFonts(i).deriveFont((float) config.getFonts(i).getSize() + slider9.getValue() - lastValue9));
    }
    else {
      b1.setFont(config.getFonts(i).deriveFont((float) config.getFonts(i).getSize() + slider16.getValue() - lastValue16));
    }
    b1.setForeground(config.getLahendus());
    b1.setBackground(config.getSudokuButton());
    b1.setText(String.valueOf(i));
    b1.setFocusPainted(false);
    b1.setLayout(new GridLayout(kast, kast));
    b1.removeAll();
    for (int k = 0; k < i; k++) {
      JLabel label;
      if (k > 9) {
        label = new JLabel("    ");
      }
      else {
        label = new JLabel("   ");
      }
      if (i == 9) {
        label.setFont(config.getFonth(i).deriveFont((float) config.getFonth(i).getSize() + slider9.getValue() - lastValue9));
      }
      else {
        label.setFont(config.getFonth(i).deriveFont((float) config.getFonth(i).getSize() + slider16.getValue() - lastValue16));
      }
      label.setForeground(config.getHelpButton());
      b1.add(label);
    }
    if (i == 9) {
      b2.setFont(config.getFonts(i).deriveFont((float) config.getFonts(i).getSize() + slider9.getValue() - lastValue9));
    }
    else {
      b2.setFont(config.getFonts(i).deriveFont((float) config.getFonts(i).getSize() + slider16.getValue() - lastValue16));
    }
    b2.setForeground(config.getLahendus());
    b2.setBackground(config.getSudokuButton());
    b2.setFocusPainted(false);
    b2.setLayout(new GridLayout(kast, kast));
    b2.removeAll();
    for (int k = 0; k < i; k++) {
      JLabel label;
      if (k > 9) {
        label = new JLabel("    ");
      }
      else {
        label = new JLabel("   ");
      }
      if (i == 9) {
        label.setFont(config.getFonth(i).deriveFont((float) config.getFonth(i).getSize() + slider9.getValue() - lastValue9));
      }
      else {
        label.setFont(config.getFonth(i).deriveFont((float) config.getFonth(i).getSize() + slider16.getValue() - lastValue16));
      }
      label.setForeground(config.getHelpButton());
      b2.add(label);
    }
  }
}
